<script setup>
import { reactive, getCurrentInstance, ref } from "vue";
import { onShow, onLoad, onUnload } from "@dcloudio/uni-app";
import listItem from "./list-item.vue";
import { getDeviceInfo, getDeviceInfoByDeviceImie } from "@/api/system";

const instance = getCurrentInstance();

const eventChannel = instance.proxy.getOpenerEventChannel();

//创建类型，false为新增，true为编辑
const searchValue = ref("");
let isShow = ref(false);
const deviceList = reactive([]);

const initData = async () => {
  uni.showLoading({ title: "加载中...", mask: true });
  uni.hideLoading();
};
initData();
onLoad(async (option) => {});
onUnload(() => {});

const showToast = (msg) => {
  uni.showToast({
    title: msg,
    icon: "none",
    duration: 2000,
    mask: true,
  });
};

// ById:根据设备id查找

const searchDevice = async (searchValue) => {
  isShow = false;
  uni.showLoading({ title: "查询中...", mask: true });
  let result;
  console.log("查询中...");

  result = await getDeviceInfo(searchValue);
  console.log("查询中Imie", result);

  uni.hideLoading();

  if (result.code == 200) {
    //deviceList = result.data;
    deviceList.length = 0
    deviceList.push(...result.data)
  } else {
    showToast("设备信息获取失败，请手动输入!");
  }
  isShow = true;
};

</script>
<template>
  <view class="container">
    <view class="">
      <uni-easyinput
        placeholder="输入用户编号/用户名/电话号码/设备ID"
        :inputBorder="false"
        v-model="searchValue"
        :clearable="false"
      />
      <view class="primaryBtn" @click="searchDevice(searchValue)">
        <text>查找</text>
      </view>
      <view v-if="isShow && deviceList.length == 0" class="">
        没有查找到相关设备，请重新输入。
      </view>
      <view v-else class="device-info-cnt">
        <view class="device-item">
          <list>
            <listItem
              v-for="(item, index) in deviceList"
              :key="item.id"
              :item="item"
            />
          </list>
        </view>
      </view>
    </view>
  </view>
</template>
<style lang="scss" scoped>
::v-deep .primaryBtn{
  height: 90rpx;
  line-height:90rpx ;
  margin-top: 4px;
}

::v-deep .uni-easyinput__content .uni-easyinput__content-input{
height:48px !important;
}

</style>
